#!/usr/bin/perl

require "getopts.pl";

&Getopts("a:3:7:T");

if ($#ARGV < 0) {
    print "Usage: $0 [options] file

Updates monit3 and monit7 geometry upper edges and monit7 title in an
Xresources file (nominally '.Xresources').

This script should be run in the directory with the file, usually the user's
home directory.

The original file is renamed as a backup by adding a '.bak' extension.
The backup file must not already exist.

Option explanations:

 -a number  set upper edge of both   windows
 -3 number  set upper edge of monit3 window
 -7 number  set upper edge of monit7 window
 -T         do not update monit7 title

The default for '-a' is 155.
If '-3' and/or '-4' for an individual window is specified, they override '-a'.
A value that is not an non-negative integer (e.g. '-1' or 'o') disables
updating that window.
";
    exit -1;
}

if  ($#ARGV < 0 || $#ARGV >0) {
    print "incorrect number of arguments\n";
    print "Try '$0' for more information.\n";
    exit -1;
}

# get the file names

$out=$ARGV[0];
if (!-e $out) {
    die "Can't find '$out', giving up.\n";
}

$bak = $out . ".bak";
if (-e $bak) {
    die "Backup-file '$bak' already exists, giving up.\n";
}

# rename the original

if(!rename ($out,$bak)) {
    print "Renaming '$out' to '$bak' failed: $!\n";
}

if(!open(BAK,$bak)) {
    print "Failed to open '$bak': $!\n";
    if(!rename($bak,$out)) {
        print "In the process of trying to restore order after the error,\n";
        print "I was unable to rename '$bak' to '$out': $!\n";
        print "Please fix whatever the problem is and rename it yourself.\n";
    } else {
        print "In order to help restore order after the error,\n";
        print "I renamed '$bak' to '$out' for you.\n";
    }
}

if(!open(OUT,">" . $out)) {
    print "Failed to open '$out': $!\n";
    if((!close(BAK)) || (!rename($bak,$out))) {
        print "In the process of trying to restore order after the error,\n";
        print "I was unable to rename '$bak' to '$out': $!\n";
        print "Please fix whatever the problem is and rename it yourself.\n";
    } else {
        print "In order to help restore order after the error,\n";
        print "I renamed '$bak' to '$out' for you.\n";
    }
}

# set widths

$all=155;
$all=$opt_a if defined($opt_a);

$monit3=$all;
$monit3=$opt_3 if defined($opt_3);
$monit3=-1 if $monit3 !~ m/^\d+$/;

$monit7=$all;
$monit7=$opt_7 if defined($opt_7);
$monit7=-1 if $monit7 !~ m/^\d+$/;

$monit7t=0;
$monit7t=-1 if defined($opt_T);

# keep count

$count{"monit3*geometry"} = 0;
$count{"monit7*geometry"} = 0;
$count{"monit7.title"} = 0;

# okay now process the file

while(<BAK>) {
    if (/^(monit3\*geometry:.*\+)\d+\s*$/) {
        $_ = "$1$monit3\n" if $monit3 >=0;
        ++$count{"monit3*geometry"};
    } elsif (/^(monit7\*geometry:.*\+)\d+\s*$/) {
        $_ = "$1$monit7\n" if $monit7 >=0;
        ++$count{"monit7*geometry"};
    } elsif (/^(monit7\.title:\s*).*$/) {
        $_ = "$1DBBC3 Tsys\n" if $monit7t >=0;
        ++$count{"monit7.title"};
    }
    print OUT;
}

foreach $key (sort keys %count) {
    if($count{$key} == 0) {
        print STDERR "WARNING: $key not found\n";
        $none=1;
    } elsif($count{$key} > 1) {
        print STDERR "ERROR: $key found more than once.\n";
        $extra=1
    }
}
if ($none || $extra) {
    print "\n";
    }
if ($none) {
    print STDERR "Some resources were missing. If you are okay with that, that is fine.\n";
}
if ($extra) {
    print STDERR "Some resources were defined more than once. There may be other Xresources
for those windows that are defined multiple times. All extra definitions should
probably be removed.
";
}
if ($none || $extra) {
    print STDERR "\nFor comparison, you can examine the default versions in
'/usr2/fs/st.default/(oper|prog)/.Xresoures' as appropriate.
";
}
